/*
 * Copyright 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yamen.bdwm.compatible;

import yamen.bdwm.MainConstant;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

/**
 * A base activity that defers common functionality across app activities to an {@link
 * ActionBarHelper}.
 *
 * NOTE: dynamically marking menu items as invisible/visible is not currently supported.
 *
 * NOTE: this may used with the Android Compatibility Package by extending
 * android.support.v4.app.FragmentActivity instead of {@link Activity}.
 */
public abstract class ActionBarActivity extends FragmentActivity {
    final ActionBarHelper mActionBarHelper = ActionBarHelper.createInstance(this);
    ProgressDialog dialog=null;
    public void onDestroy()
    {
    	if(dialog!=null)
    		dialog.dismiss();
    	super.onDestroy();
    }
    public void onResume()
	 {
		 super.onResume();
		 MainConstant.nowActivity=this;
	 }
    public void SetRefreshingDialog(boolean refreshing)
    {
    	
        if(refreshing)
        {
        	if(dialog==null)
        	{
	        	dialog= new ProgressDialog(this);
	            dialog.setMessage(getResources().getString(yamen.bdwm.R.string.loading));
	            dialog.setIndeterminate(true);
	            dialog.setCancelable(false);
        	}
        	dialog.show();
        	
        }
        else
        	if(dialog!=null)
        		dialog.dismiss();
    }

    /**
     * Returns the {@link ActionBarHelper} for this activity.
     */
    public ActionBarHelper getActionBarHelper() {
        return mActionBarHelper;
    }

    /**{@inheritDoc}*/
    @Override
    public MenuInflater getMenuInflater() {
        return mActionBarHelper.getMenuInflater(super.getMenuInflater());
    }

    /**{@inheritDoc}*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBarHelper.onCreate(savedInstanceState);
        //if(MyData.getInstance()!=null)
        //this.setTitle(getResources().getString(R.string.app_name)+" - " + MyData.getInstance().getUserName());
    }

    /**{@inheritDoc}*/
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mActionBarHelper.onPostCreate(savedInstanceState);
    }
    /**{@inheritDoc}*/
    @Override
    public boolean onPrepareOptionsMenu (Menu menu)
    {
    		myMenu=menu;
    	  boolean retValue = false;
          retValue |= mActionBarHelper.onPrepareOptionsMenu(menu);
          retValue |= super.onPrepareOptionsMenu(menu);
          return retValue;
    }
    /**
     * Base action bar-aware implementation for
     * {@link Activity#onCreateOptionsMenu(android.view.Menu)}.
     *
     * Note: marking menu items as invisible/visible is not currently supported.
     */
    public Menu myMenu;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	myMenu=menu;
        boolean retValue = false;
        retValue |= mActionBarHelper.onCreateOptionsMenu(menu);
        retValue |= super.onCreateOptionsMenu(menu);
        return retValue;
    }

    /**{@inheritDoc}*/
    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        mActionBarHelper.onTitleChanged(title, color);
        super.onTitleChanged(title, color);
    }
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId())
		{
		case android.R.id.home:
			this.onBackPressed();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
    
    
}
